import Vue from "vue";
import Router from "vue-router";


Vue.use(Router);

const router = new Router({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [
    {
      path: "/",
      name: "index",
      redirect: "datatotal",
      component: () => import("@/views/Index"),
      children: [
        {
          path: "vipmanger",
          name: "vipmanger",
          component: () => import("@/views/VipManger")
        },
        {
          path: "goodslist",
          name: "goodslist",
          component: () => import("@/views/GoodsList")
        },
        {
          path: "productlist",
          name: "productlist",
          component: () => import("@/views/ProductList")
        },
        {
          path: "items",
          name: "items",
          component: () => import("@/views/ItemsList")
        },
        {
          path: "daili",
          name: "daili",
          component: () => import("@/views/DaiLi")
        },
        {
          path: "jurisdiction",
          name: "jurisdiction",
          component: () => import("@/views/JurisDiction")
        },
        {
          path: "menulist",
          name: "menulist",
          component: () => import("@/views/MenuList")
        },
        {
          path: "masterlist",
          name: "masterlist",
          component: () => import("@/views/MasterList")
        },
        {
          path: "secondMenber/:userid",
          name: "secondMenber",
          component: () => import("@/views/SecondMenber")
        },
        {
          path: "total/:userid",
          name: "total",
          component: () => import("@/views/eCharts")
        },
        {
          path: "questen",
          name: "questen",
          component: () => import("@/views/UserQuesten")
        },
        {
          path: "xieyi",
          name: "xieyi",
          component: () => import("@/views/XieyiList")
        },
        {
          path: "addxieyi",
          name: "xieyilist",
          component: () => import("@/views/XieYi")
        },
        {
          path: "datatotal",
          name: "datatotal",
          component: () => import("@/views/DataTotal")
        }
      ]
    },
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/Login")
    },
    
    {
      path: "/Error404",
      name: "error",
      component: () => import("@/views/ErrorPage")
    }
  ]
});

router.beforeEach( (to, from, next) => {
  let token = window.localStorage.miniToken
  if (!token && to.path != "/login") {
    next("/login")
  } else {
    if (to.matched.length === 0) { // 未找到匹配路由
        next("/Error404")
        setTimeout(() => {
          from.path ? next({path: from.path}) : next("/")
        }, 3000)
        
    } else {
      next()
    }
  }

  
  
})

export default router;
